$(function () {

     //address
     $(".consignee-main ul li:gt(1)").hide();
     $(".more-address span").click(function () {
         $(this).parent(".more-address").toggleClass("current");
         $(".consignee-main ul li:gt(1)").fadeToggle(50)
     })
     $(".consignee-main ul li").click(function () {
         $(this).addClass("current").siblings().removeClass("current");
     })

     //cart address
     $(".add-search span").click(function(){
		var url = $(this).attr('data-url');
        $.get(url, function(rs){
            $('#addressLayer').html(rs);
			$(".total-mask,.total-content").show();
            closeAddrLayer();
            addAddress();
		});
    })
    var closeAddrLayer = function(){
        $(".cart-d-icon i,.cart-d-gary").click(function(){
            $(".total-mask,.total-content").hide()
        })        
    };

    //添加地址
	var addAddress = function(){
        $('.cart-d-color').click(function(){
            var url = $(this).attr('data-url');
            $.post(url, $('#addressForm').serialize(), function(rs){
                if(rs.error==0){
                    location.reload();
                }else if(rs.error==1){
                    alert('error');
                }
            });
        });
    }

    //设置默认地址
    var setDefaultAddr = function(){
        $('.consignee-other .default').click(function(){
            var url = $(this).attr('data-url');
            var addrId = $(this).attr('addrId');
            var currObj = $(this);
            $.get(url, function(rs){
                if(rs.error==0){
                    //选中效果
                    $('.consignee-main ul li').removeClass('current');
                    $('#li_'+addrId).addClass('current');
                    //设为默认地址
                    $('.consignee-other .default').text('设为默认地址');
                    currObj.text('');
                }else if(rs.error==1){
                    alert('error');
                }
            });
        });    
    }
    setDefaultAddr();

    //修改地址
    var editAddr = function(){
        $('.revise-consignee').click(function(){
            var url = $(this).attr('data-url');
            $.get(url, function(rs){ 
                $('#addressLayer').html(rs);
                $(".total-mask,.total-content").show();
                closeAddrLayer();
                addAddress();
            });
        });
    }
    editAddr();

    //按姓名或手机号搜索地址
    var searchAddr = function(){
        var wd = $('.add-input').val().trim(); //去空格
        if(wd=="") return false;
        $.get('/member/address/search', {wd:$('.add-input').val()}, function(rs){
            $('.consignee-main').html(rs);
            $('.add-input').val(wd); //去空格后重新赋值
            
            $(".consignee-main ul li:gt(1)").hide();
            $(".more-address span").click(function () {
                $(this).parent(".more-address").toggleClass("current");
                $(".consignee-main ul li:gt(1)").fadeToggle(50);
            })
            $(".consignee-main ul li").click(function () {
                $(this).addClass("current").siblings().removeClass("current");
            })
            
            setDefaultAddr();
            editAddr();
        });
    };
    //点击搜索按钮
    $('.add-btn').click(function(){
        searchAddr();
    });
    //回车键搜索
    $('.add-input').keydown(function(){
        if(event.keyCode == 13) searchAddr();
    });
})